<?php

namespace App\Logic\Auth\Form;

use App\Core\Exception\DefaultException;
use App\Model\Auth\MenuModel;

class MenuSaveForm extends \App\Core\BaseForm{
    
    public $id;
    public $title;
    public $identification ;
    public $pid = 0;
    public $sort = 0;
    public $state = 1;
    public $type = 1;
    public $web_path;
    public $icon;
    public $menu_type;

    public function rules(){
        return [
            'title'          => ['required|notEmpty|mbLengthMax:100', '类目名称', '请输入1——100字符的内容'],
            'identification' => ['required|notEmpty', '标识', '请正确输入标识'],
            'pid'            => ['required', '父级菜单'],
            'sort'           => ['required|integer|min:0', '排序', '请输入0——99999字符纯数字可重复'],
            'state'          => ['required', '是否显示', '请选择是否显示'],
            'web_path'       => ['required', '前端路由', '请正确输入前端路由'],
            'icon'           => ['required', '图标', '请正确图标'],
            'menu_type'      => ['required', '菜单类型', '请正确菜单类型'],
        ];
    }

    protected $_info;
    
    /**
     * 校验提交数据
     * @return bool
     * @throws DefaultException
     * @throws \EasySwoole\Validate\Exception\Runtime
     */
    public function validate(){
        //生成验证器，方法自动读取rules方法返回的值数据
        $validate = $this->createValidator();

        //补充一些没法通过配置的规则，如inArray
        $booleanArr = [MenuModel::ENABLED, MenuModel::DISABLED];
        $validate->addColumn('state')->inArray($booleanArr);

        $booleanArr = [MenuModel::TYPE_VIEW, MenuModel::TYPE_BUTTON];
        $validate->addColumn('type')->inArray($booleanArr);

        $booleanArr = [MenuModel::MENU_TYPE_INSIDE, MenuModel::MENU_TYPE_OUTSIDE];
        $validate->addColumn('menu_type')->inArray($booleanArr);
        
        if(!($check = $validate->validate($this->getAttributes()))){
            throw new DefaultException($validate->getError()->__toString(), 600);
        }
        
        if($this->id && empty($this->getInfo())){
            throw new DefaultException('不存在记录');
        }

        return true;
    }
    
    /**
     * 保存数据
     * @return \App\Core\Output
     * @throws DefaultException
     * @throws \EasySwoole\Mysqli\Exception\Exception
     * @throws \EasySwoole\ORM\Exception\Exception
     * @throws \EasySwoole\Validate\Exception\Runtime
     * @throws \Throwable
     */
    public function saveData(){
        $this->validate();
        
        if($this->id){
            $result = MenuModel::updateData($this->getAttributeData(['id']), ['id' => $this->id]);
        }else{
            $result = MenuModel::insertData($this->getAttributeData(['id']));
        }
        
        return $this->success(['id' => $this->id ?: $result->lastQueryResult()->getLastInsertId()]);
    }
    
    /**
     * 获取详情
     * @return \App\Core\BaseModel|array|bool|\EasySwoole\ORM\AbstractModel|\EasySwoole\ORM\Db\Cursor|\EasySwoole\ORM\Db\CursorInterface|null
     * @throws \EasySwoole\Mysqli\Exception\Exception
     * @throws \EasySwoole\ORM\Exception\Exception
     * @throws \Throwable
     */
    public function getInfo(){
        if($this->_info === null){
            $this->_info = MenuModel::getOne(['id' => $this->id]);
        }
        return $this->_info;
    }
    
}